﻿/********************************************************************
	日期:2022/4/28 21:59
	名称:NetSvc
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:网络服务
*********************************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PENet;
using PEProtocol;

public class NetSvc : MonoBehaviour{
    public static NetSvc Instance = null;

    PESocket<ClientSession, GameMsg> client = null;

    private static readonly string obj = "lock";
    private Queue<GameMsg> msgQue = new Queue<GameMsg>();

    public void InitSvc(){
        Instance = this;

        client = new PESocket<ClientSession, GameMsg>();

        client.SetLog(true, (string msg, int lv) => {
            switch (lv){
                case 0:
                    msg = "Log:" + msg;
                    Debug.Log(msg);
                    break;
                case 1:
                    msg = "Warn:" + msg;
                    Debug.LogWarning(msg);
                    break;
                case 2:
                    msg = "Error:" + msg;
                    Debug.LogError(msg);
                    break;
                case 3:
                    msg = "Info:" + msg;
                    PECommon.Log(msg);
                    break;
            }
        });

        client.StartAsClient(ClientCfg.svcIP, ClientCfg.svcPort);

        PECommon.Log("Init NetSvc...");
    }

    public void AddNetPkt(GameMsg msg){
        lock (obj){
            msgQue.Enqueue(msg);
        }
    }

    public void SendMsg(GameMsg msg){
        if (client.session == null){
            GameRoot.AddTips("服务器未连接");
            InitSvc();
            return;
        }

        client.session.SendMsg(msg);
    }

    private void Update(){
        if (msgQue.Count == 0){
            return;
        }

        lock (obj){
            GameMsg msg = msgQue.Dequeue();
            ProcessMsg(msg);
        }
    }

    private void ProcessMsg(GameMsg msg){
        if (msg.err != (int) ErrorCode.None){
            switch ((ErrorCode) msg.err){
                case ErrorCode.AcctIsOnLine:
                    GameRoot.AddTips("当前账号已经在线");
                    break;
                case ErrorCode.WrongPass:
                    GameRoot.AddTips("账号或密码错误");
                    break;
                case ErrorCode.NameIsExists:
                    GameRoot.AddTips("该名称已存在");
                    break;
                case ErrorCode.UpdateDBError:
                    GameRoot.AddTips("网络不稳定");
                    PECommon.Log("数据库更新异常", LogType.Error);
                    break;
                case ErrorCode.ServerDataError:
                    PECommon.Log("数据库更新异常", LogType.Error);
                    GameRoot.AddTips("服务器数据异常");
                    break;
                case ErrorCode.LackCoin:
                    GameRoot.AddTips("金币不足");
                    break;
                case ErrorCode.LackCrystal:
                    GameRoot.AddTips("水晶不足");
                    break;
                case ErrorCode.LackLevel:
                    GameRoot.AddTips("等级不够");
                    break;
                case ErrorCode.LackDiamond:
                    GameRoot.AddTips("钻石数量不够");
                    break;
                case ErrorCode.ClientDataError:
                    PECommon.Log("客户端数据异常", LogType.Error);
                    break;
            }

            return;
        }

        switch ((CMD) msg.cmd){
            case CMD.ResLogin:
                LoginSys.Instance.ResLogin(msg);
                break;
            case CMD.ResRename:
                LoginSys.Instance.ResRename(msg);
                break;
            case CMD.ResGuide:
                MainCitySys.Instance.ResGuide(msg);
                break;
            case CMD.ResStrong:
                MainCitySys.Instance.ResStrong(msg);
                break;
            case CMD.PshChat:
                MainCitySys.Instance.PshChat(msg);
                break;
            case CMD.ResBuy:
                MainCitySys.Instance.ResBuy(msg);
                break;
            case CMD.PshPower:
                MainCitySys.Instance.PshPower(msg);
                break;
            case CMD.ResTakeTaskReward:
                MainCitySys.Instance.ResTakeTaskReward(msg);
                break;
            case CMD.PshTaskPrg:
                MainCitySys.Instance.PshTaskPrg(msg);
                break;
            case CMD.ResFBFight:
                FuBenSys.Instance.ResFBFight(msg);
                break;
        }
    }
}
